package com.jingdianjichi.subject.domin.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domin.convert.SubjectInfoConverter;
import com.jingdianjichi.subject.domin.convert.SubjectJudgeConverter;
import com.jingdianjichi.subject.domin.convert.SubjectMultipleConverter;
import com.jingdianjichi.subject.domin.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domin.entity.SubjectInfoBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.service.SubjectInfoService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目的策略类
 * @author 86156
 * @version 1.0
 * @date 2024-10-21 13:57
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{
    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectInfoService subjectInfoService;

    @Override
    public SubjectInfoTypeEnum getSubjectType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    /**
     * 多选题插入
     * @param subjectInfoBO
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectMultiple> subjectMultiples = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option->{
            SubjectMultiple subjectMultiple = SubjectMultipleConverter.instance.convertBoToEntity(option);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectMultiples.add(subjectMultiple);
        });
        subjectMultipleService.insertBatch(subjectMultiples);
    }

    @Override
    public SubjectInfoBO query(Long subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(subjectId);
        List<SubjectMultiple> subjectMultipleList = subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOList =
                SubjectMultipleConverter.instance.convertMultipleToAnswerList(subjectMultipleList);
        SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
        subjectInfoBO.setOptionList(subjectAnswerBOList);
        return subjectInfoBO;
    }
}
